package com.banksoft.admin.controller;

import com.banksoft.admin.bean.CityBean;
import com.banksoft.admin.entity.User;
import com.banksoft.admin.service.UserService;
import com.banksoft.common.BaseController;
import com.banksoft.util.CityUtil;
import com.banksoft.util.StringUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by vanjoger on 2017/1/4.
 */
@Controller
@RequestMapping("/front")
public class CommonController extends BaseController{

    @Autowired
    UserService userService;

    private String[] arrays = new String[]{"青海","西藏","新疆","内蒙","甘肃","宁夏","海南"};

    @RequestMapping("/loadCitys")
    @ResponseBody
    public JSONObject loadCitys(String pid){
        List<CityBean> list = new ArrayList<>();
        //青海，西藏，新疆，内蒙，甘肃，宁夏，海南
        if("0".equals(pid)){
            list = CityUtil.getCitysByLevel("1");
        }else{
            list =  CityUtil.getCitysByPNo(pid);
        }
       /* if(StringUtil.isNotEmpty(list)){
            for(int i=list.size()-1;i>=0;i--){
                CityBean bean = list.get(i);
                boolean exist = false;
                for(String str:arrays){
                    if(bean.getName().indexOf(str)>-1){
                        exist = true;
                        break;
                    }
                }
                if(exist)
                    list.remove(i);
            }
        }*/
        return this.FmtResult(true,null,list);
    }


    @RequestMapping("/listSelector")
    public String listSelector(){
        return "selector";
    }

    @RequestMapping("/listSelector1")
    public String listSelector1(){
        return "selector1";
    }


    @RequestMapping("/listUser")
    @ResponseBody
    public JSONObject listUser(String name){
        try{
            List<User> list = this.userService.listByName(name);
            return this.FmtResult(true,null,list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return this.FmtResult(false,null,null);
    }
}
